//
//  LoginViewController.m
//  Logistics
//
//  Created by 华谕 on 2017/10/31.
//  Copyright © 2017年 HUYU. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForGoPasswordViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
{
    UITextField *phoneField;
    UITextField *passwordField;
}
@end

@implementation LoginViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(Successnotice:) name:@"bangdingsuccess" object:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"bangdingsuccess" object:nil];
}

- (void)Successnotice:(NSNotification *)center
{
    [LTools showMBProgressWithText:@"绑定成功" addToView:sharedDelegate.window];
    HYTabBarViewController * tabBarVC = [[HYTabBarViewController alloc]init];
    sharedDelegate.tabbarViewController = tabBarVC;
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
    [LTools showMBProgressWithText:@"登录成功" addToView:sharedDelegate.window];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}

- (void)creatUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:Icon(@"backimg") style:UIBarButtonItemStylePlain target:self action:@selector(dismissVc)];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"专线网";
    NSArray *imgAry = @[@"my_icon04",@"my_icon05"];
    NSArray *pladerAry = @[@"手机号",@"密码"];
    for (int i = 0; i < pladerAry.count; i ++)
    {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 20*displayheight+i*55*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight)];
        backView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:backView];
        UIImageView *imageview = [ZCControl createImageViewWithFrame:CGRectMake(5*displaywidth, 12.5*displayheight, 20*displaywidth, 20*displayheight) ImageName:imgAry[i]];
        imageview.contentMode = UIViewContentModeScaleAspectFit;
        [backView addSubview:imageview];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 12.5*displayheight, 1, 20*displayheight)];
        line.backgroundColor = RGB(204, 204, 204);
        [backView addSubview:line];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(40*displaywidth, 2.5*displayheight, SCREEN_WIDTH-70*displaywidth, 40*displayheight) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:15.0];
        [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.delegate = self;
        textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
        textfield.textColor = RGB(48, 48, 48);
        textfield.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:textfield];
        if (i == 0)
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            phoneField = textfield;
        }
        else
        {
            textfield.secureTextEntry = YES;
            passwordField = textfield;
        }
    }
    UIButton *forgoBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-135*displaywidth, 130*displayheight, 120*displaywidth, 20*displayheight) ImageName:nil Target:self Action:@selector(forGoAction) Title:@"忘记密码？" Font:11.0];
    forgoBtn.backgroundColor = [UIColor clearColor];
    [forgoBtn setTitleColor:RGB(135, 135, 135) forState:UIControlStateNormal];
    forgoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    forgoBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:forgoBtn];
    
    UIButton *loginBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 180*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(loginBtn) Title:@"登录" Font:18.0];
    loginBtn.backgroundColor = RGB(255, 153, 20);
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.layer.masksToBounds = YES;
    [self.view addSubview:loginBtn];
    
    UIButton *registerBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 235*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(registerBtn) Title:@"注册" Font:18.0];
    registerBtn.backgroundColor = RGB(204, 204, 204);
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerBtn.layer.cornerRadius = 5;
    registerBtn.layer.masksToBounds = YES;
    [self.view addSubview:registerBtn];
    
    if ([WXApi isWXAppInstalled])
    {
        UILabel *otherlog = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH/2-35*displaywidth, 365*displayheight, 70*displaywidth, 20*displayheight) Font:11.0 Text:@"第三方登录" Color:RGB(128, 128, 128)];
        otherlog.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:otherlog];
        UIView *leftview = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 374.5*displayheight,SCREEN_WIDTH/2- 85*displaywidth, 1*displayheight)];
        leftview.backgroundColor = RGB(204, 204, 204);
        [self.view addSubview:leftview];
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2+50*displaywidth, 374.5*displayheight, SCREEN_WIDTH/2- 85*displaywidth, 1*displayheight)];
        rightView.backgroundColor = RGB(204, 204, 204);
        [self.view addSubview:rightView];
        UIButton *button = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2 - 27.5*displayheight, 400*displayheight, 55*displayheight, 55*displayheight) ImageName:@"wxin" Target:self Action:@selector(ThirdLogin) Title:nil Font:14.0];
        [self.view addSubview:button];
    }
}

//忘记密码
- (void)forGoAction
{
    [self.view endEditing:YES];
    ForGoPasswordViewController *vc = [[ForGoPasswordViewController alloc] init];
    vc.titleString = @"忘记密码";
    [self.navigationController pushViewController:vc animated:YES];
    
}

//登录
- (void)loginBtn
{
    [self.view endEditing:YES];
    if (phoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (passwordField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入密码" addToView:self.view];
        return;
    }
    WEAKSELF;
    NSDictionary *dic = @{@"phone":phoneField.text,
                          @"password":passwordField.text
                          };
    [LTools cache:@"" ForKey:SERVER_LoginToken];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestLoginUserParmers:dic Success:^(id obj) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        if ([obj server_Getcodecheck])
        {
            NSString *token = obj[@"data"][@"token"];
            [LTools storage:YES ForKey:SERVER_IsLogin];
            [LTools cache:token ForKey:SERVER_LoginToken];
            NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
            [dictionary addEntriesFromDictionary:obj];
            NSDictionary *reloadDic = [NSDictionary changeType:dictionary];
            [LTools cache:reloadDic ForKey:SERVER_LoginData];
            UserAccountModel *model = [UserAccountModel shareMange];
            [model jsonUserData:reloadDic];
            HYTabBarViewController * tabBarVC = [[HYTabBarViewController alloc]init];
            sharedDelegate.tabbarViewController = tabBarVC;
            [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
            [LTools showMBProgressWithText:@"登录成功" addToView:sharedDelegate.window];
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];

    }];
}

//注册
- (void)registerBtn
{
    [self.view endEditing:YES];
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//微信登录
- (void)ThirdLogin
{
    [self.view endEditing:YES];
    SendAuthReq* req =[[SendAuthReq alloc]init] ;
    req.scope = @"snsapi_userinfo" ;
    req.state = @"Applogin" ;
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];
}

- (void)dismissVc
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
